package cn.com.inossem.operation;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.network.NetworkConnect;
import cn.com.inossem.requestmanager.WooRequestFactory;

import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;
import com.google.gson.Gson;

/**
 * 上传偏好接口
 * 
 * @author wuyt3
 */
public class PreferencesOperation implements Operation {
	public static final String PREFERENCES_OPERATION_STR = "preferences_operation_str";
	private static final String TAG = "MessageListOperation";

	@Override
	public Bundle execute(Context context, Request request)
			throws ConnectionException, DataException, CustomRequestException {
		Gson gson = new Gson();
		String preferences = request.getString(PREFERENCES_OPERATION_STR);
		NetworkConnect connection = new NetworkConnect(WSConfig.LOCAL_URL
				+ "projectInfo/addPreferences");
		System.out.println("preferences:" + preferences);
		connection.setPostText(preferences);

		String body = connection.execute();
		System.out.println("PreferencesOperation:" + body);

		Bundle bundle = new Bundle();

		try {
			JSONObject jsonObject = new JSONObject(body);
			int errorFlag = jsonObject.getInt("errorFlag");

			if (errorFlag == WSConfig.RESULT_SUCESS) // 成功
			{
			} else {
			}
			// 返回项目机会
			bundle.putInt("result", errorFlag);

			// 设置登陆结果
			bundle.putInt(WooRequestFactory.BUNDLE_PREFERENCES_RESULT,
					errorFlag);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return bundle;
	}

}
